package com.univrz.flexmachine.scxml.model
{
	
	
	
	
	public class StateNode extends Node implements ITransitionTarget
	{
	
	
	
	

		public function StateNode( parent:Node = null, initialSubState:Node = null )
		{
			
			// if an initial substate is passed, we create an implicit internal
			// InitialNode subnode.
			// with an empty transition
			if ( initialSubState )
			{
				children.push( createInitial( initialSubState ) );
			}
			
		}
		




		private var _initial:InitialNode;
		
		public function get initial( ):InitialNode
		{
			
			if ( _initial == null )
			{
				
				if ( getChildrenOfType( InitialNode ).length == 0 )
				{
					// create initial pointing to first child ( is this formally correct? )
					children.push( createInitial( children[0] ) );
				}
				
				_initial = getChildrenOfType( InitialNode )[0];
				
			}
					
			return _initial;
		}




		private function createInitial( target:Node ):InitialNode
		{
			var initial:InitialNode = new InitialNode( this );
			new TransitionEdge( initial, true, null, [ target ] );	
			return initial;	
		}







		/**
		 * 
		 * 
		 * 
		 * 
		 * 
		 * @param state
		 * @param transitions
		 * @return 
		 * 
		 */
		internal function isPreempted( transitions:Array ):Boolean
		{
			for each ( var t:TransitionEdge in transitions )
				if ( t.targets.length > 0 )
        			if ( isDescendantOf( t.sourceAndTargetsLCA ) )
           				return true;
   			return false;
		}
		
		
		
		
	}
}